package org.kaer.election.ui;

import android.os.Bundle;
import android.view.KeyEvent;

import org.kaer.election.R;
import org.kaer.election.fragment.ElectionFragment;
import org.kaer.election.utils.InputMethodUtils;
import org.kaer.election.utils.NetworkUtil;

/**
 * Created by lizj on 2016/7/7.
 */
public class ElectionActivity extends BaseActivity {
    private ElectionFragment electionFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        initWidget();
        initData();
    }


    private void initWidget() {

    }

    private void initData() {
        electionFragment = ElectionFragment.newInstance(mBundle);
        addFragment(electionFragment);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!NetworkUtil.isAvailable(this)) {
            finish();
        }
    }

    public void updateElectionBundle(Bundle bundle) {
        if (electionFragment == null)
            electionFragment = ElectionFragment.newInstance(mBundle);
        electionFragment.updateBundle(bundle);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_election;
    }

    @Override
    protected int getFragmentContentId() {
        return R.id.content;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            InputMethodUtils.hideInputMethod(ElectionActivity.this);
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
                removeFragment();
            } else
                finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
